package com.example.housingRentalSystem.service.Impl;

import com.example.housingRentalSystem.bean.Collect;
import com.example.housingRentalSystem.bean.House;
import com.example.housingRentalSystem.mapper.CollectMapper;
import com.example.housingRentalSystem.mapper.HouseMapper;
import com.example.housingRentalSystem.service.CollectService;
import com.example.housingRentalSystem.utils.PageRequest;
import com.example.housingRentalSystem.utils.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author 王硕
 * @version jdk21
 * @className CollectServiceImpl
 * @description
 * @date 2025/3/22 17:04
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;


    @Override
    public boolean toggleCollect(Integer userId, Integer houseId) {

        if (collectMapper.checkCollected(userId, houseId) > 0) {
            collectMapper.deleteCollect(userId, houseId);
            return false;
        } else {
            Collect collect = new Collect();
            collect.setUserId(userId);
            collect.setHouseId(houseId);
            collectMapper.insertCollect(collect);
            return true;
        }
    }

    @Override
    public PageResponse<House> getCollectList(Integer userId, PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getSize());
        Page<House> page = collectMapper.selectCollectList(userId);
        return PageResponse.build(page);
    }

    @Override
    public boolean isCollected(Integer userId, Integer houseId) {
        return collectMapper.checkCollected(userId, houseId) > 0;
    }
}
